package com.juyu.miliao.api.dao.mysql;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jh
 * 2018/8/24
 */
@Repository
public class UserLoginDao {

    @Resource
    private SqlSession sqlSession;


    public int updateUserLoginAge(Integer age, Long userId) {
        if (age != null && age >= 0 && userId != null) {
            Map<String, Object> map = new HashMap<>(4);
            map.put("age", age);
            map.put("userId", userId);
            return sqlSession.update("updateUserLoginAge", map);
        }
        return 0;
    }

    public List<String> selectPhone(long userId, String phone) {
        Map<String, Object> loginMap = new HashMap<>(2);
        loginMap.put("userId", userId);
        loginMap.put("phone", phone);
        return sqlSession.selectList("selectUserLoginByPhone", loginMap);
    }

    public int updateMinorModeSwitch(long userId, int minorModeSwitch) {
        Map<String, Object> minorModeMap = new HashMap<>(2);
        minorModeMap.put("userId", userId);
        minorModeMap.put("minorModeSwitch", minorModeSwitch);
        return sqlSession.update("updateMinorModeSwitch", minorModeMap);
    }

    public String selectUserLoginById(long id) {
        return sqlSession.selectOne("selectUserLoginById", id);
    }

    public String selectUserLoginByIdForEnable(long id) {
        return sqlSession.selectOne("selectUserLoginByIdForEnable", id);
    }

    public void updateUserLoginUserNameAndPassWord(Long userId, String phone, String password) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("userId", userId);
        map.put("phone", phone);
        map.put("password", password);
        sqlSession.update("updateUserLoginUserNameAndPassWord", map);
    }

    public String getPhoneByUserId(long id) {
        return sqlSession.selectOne("getPhoneByUserId", id);
    }

    public int checkPhone(String phoneNum) {
        return sqlSession.selectOne("userLoginCheckPhone", phoneNum);
    }


}
